var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curOrderId:'',
    page:1,
    isDisable:false,
    list:[],
    currentTab: 1
  },
  onShow(){
    this.getList()
  
  },
  getList(){
    common.showLoading();
    common.hideLoading();
    server.postJSON(common.storePath + 'ecinvoice/index/', {
      "page": this.data.page,
      "count": 10,
    }, res => {
      if (res.data.errno == '0') {
        this.setData({
          list: res.data.data,
          isEmpty: res.data.data.length > 0 ? false : true
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  switchNav(e) {
    var statusId = e.currentTarget.dataset.status;
    this.setData({
      currentTab: statusId,
      page: 1,
      list: []
    })
    this.getList();
  },
  openInvoice() {
    this.setData({
      isDisable:true
    })
    server.postJSON(common.storePath + 'ecinvoice/apply/', {
      "order_id": this.data.curOrderId
    }, res => {
      if (res.data.errno == "0") {
        wx.navigateTo({
          url: '../common/gzh?url=https://nnfp.jss.com.cn/invoice/qyScan/qyScan_UI.action?jskpCode=CM7264&orderno=1904181000284&scanModel=scan&expiretime=1560418491000',
        })
        this.setData({
          isDisable: false
        })
      }else{
        this.setData({
          isDisable: false
        })
        common.showErrorToast(res.data.errmsg)
      }
    })
  },
  selectInvoice(e){
    let orderId=e.currentTarget.dataset.orderid;
    this.setData({
      curOrderId:orderId
    })
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    let that = this;
    if (that.data.list.length > 4) {
      common.showLoading();
      common.hideLoading();
      that.setData({
        page: that.data.page + 1 //每次触发上拉事件，把searchPageNum+1 
      });
      that.getMoreData()
    }
  },
  getMoreData() {
    server.postJSON(common.storePath + 'ecinvoice/index/', {
      "page": this.data.page,
      "count": 10
    }, res => {
      console.log(res)
      if (res.data.errno == "0") {
        common.hideLoading();
        if (res.data.data.length > 0) {
          console.log(res.data.data)
          var newData = this.data.list;
          newData = newData.concat(res.data.data)
          this.setData({
            list: newData,
            isEmpty: res.data.data.length > 0 ? false : true
          })
        }
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },

})